#!/bin/bash
# Author: zhoubangjun
# 0  1 * * * /bin/bash /root/script/es_index_config_slow_query.sh &>> /var/log/es_index_config_slow_query.log

date_format1=$(date +%Y.%m.%d)
date_format2=$(date +%Y-%m-%d)
date_format3=$(date +%Y%m%d)
es_info_file="/root/script/.es_info"
[[ ! -f ${es_info_file} ]] && echo "file not exist: ${es_info_file}" && exit 1
ES_HOST_PORT=$(awk '{print $1}' ${es_info_file})
USERNAME=$(awk '{print $2}' ${es_info_file})
PASSWORD=$(awk '{print $3}' ${es_info_file})

get_index_list=$(curl -s -u "${USERNAME}:${PASSWORD}" ${ES_HOST_PORT}/_cat/indices?v | grep -vE 'monitor|kibana' | grep -E "${date_format1}|${date_format2}|${date_format3}" | awk '{print $3}')
for index in ${get_index_list[*]}
do
        echo config index slow query, indexname ${index}
        curl -u "${USERNAME}:${PASSWORD}" http://${ES_HOST_PORT}/${index}/_settings -X PUT  -H 'Content-Type: application/json' -d '
                {"index.search.slowlog.threshold.query.warn":"3s",
                "index.search.slowlog.threshold.fetch.warn":"3s",
                "index.indexing.slowlog.threshold.index.warn":"3s",
                "index.indexing.slowlog.level":"info",
                "index.indexing.slowlog.source":"1000"}
        '
done
